#!/bin/bash
update_conf_path=`dirname $0`
source $update_conf_path/cp_ctl_conf.sh

function pash_list
{
		for i in ${_arr_1[@]};
		do
				#echo $i
				mkdir -p $i
		done
		for j in ${_arr_2[@]};
		do
				#echo $j
				mkdir -p $j
		done


}


function json_conf_rsync
{
#ls -l $game_config_json_path/
#ls -l $game_goconfig_json_path/
echo 配置同步列表::
rsync -azP $game_config_json_path/* $server_config_json_path | grep json
rsync -azP $game_goconfig_json_path/* $server_goconfig_json_path | grep json

}
	
function release_update
{
echo ""
echo ------UPDATE FILE LIST-----
up=`cat $release_path/$u_file_list/up_f | wc -l`
cl=`cat $release_path/$u_file_list/cl_f | wc -l`

cat $release_path/$u_file_list/up_f
echo ------CLEAR FILE LIST-----
cat $release_path/$u_file_list/cl_f
echo ""
echo ------GAME_UPDATE-----
        for ((i=1; i<=$up; i++))
                do
                path_update=`cat $release_path/$u_file_list/up_f | awk NR==$i |cut -b 2-`
                dir_path=`dirname $path_update`
                if [ ! -d $game_path/$dir_path ];then
                        mkdir -p $game_path/$dir_path
                fi
                echo $release_path/$date_file/$Hb_t/$path_update $game_path/$path_update
                cp $release_path/$date_file/$Hb_t/$path_update $game_path/$path_update
        done
        for ((i=1; i<=$cl; i++))
                do
                path_cl=`cat $release_path/$u_file_list/cl_f | awk NR==$i |cut -b 2-`
                #echo clear: $game_path/$path_cl
                rm -rf $game_path/$path_cl

        done

}

#配置文件更新
function server
{
rm -rf $game_path/gameserver/application/lib/config
/usr/local/php7/bin/php $game_path/gameserver/config/initConfig.php $server_config_json_path/$config_json $server_path
cp -r $server_path $game_path/gameserver/application/lib/
}


function server_go
{
rm -rf $game_path/goserver/bin/conf/*.json
/usr/local/php7/bin/php $game_path/gameserver/config/initGoConfig.php $server_goconfig_json_path/$config_gojson $server_go_path
cp -r $server_go_path/*.json $game_path/goserver/bin/conf/
}

function json_config_name
{
#ls -l $server_config_json_path/ | grep config | awk '{print $9}'
ls -l $server_config_json_path | awk '{print $9}'
config_json=`cat $release_path/config_json_t`
echo -n -e "请输入对应服务器配置文件名称:默认 \033[31m $config_json: \033[0m : 无修改请直接回车:::"
read config_json
if [ -z $config_json ];then
	config_json=`cat $release_path/config_json_t`
fi
echo $config_json
last_t_continue '请确认:'

}

function gojson_config_name
{
ls -l $server_goconfig_json_path  | awk '{print $9}'
config_gojson=`cat $release_path/config_gojson_t`
echo -n -e "请输入对应服务器配置文件名称:默认 \033[31m $config_gojson: \033[0m : 无修改请直接回车:::"
read config_gojson
if [ -z $config_gojson ];then
        config_gojson=`cat $release_path/config_gojson_t`
fi
cat $release_path/config_gojson_t
echo $config_gojson
last_t_continue '请确认:'
}



#------------------------------------------------------------
function y_u
{
echo -e "\033[31m 服务器rsync同步版本库代码: \033[0m"
pssh  -h $iptxt_path  $update_code_path/cp_z.sh -y | tee $update_code_path/pssh-rs
echo ''
echo 成功rsync同步数:`cat $update_code_path/pssh-rs | grep SUCCESS | wc -l`
ssh -p $ssh_port $account_ip  $update_code_path/cp_z.sh -z
ssh -p $ssh_port $account_ip  $update_code_path/cp_z.sh -m > $md5_file_path/md5_FZ_price.md5
FZ_MD5=`cat $md5_file_path/md5_FZ_price.md5 | grep -v SUCCESS | grep -v ^$`
echo $FZ_MD5
last_t_continue "是否更新同服其它代码服务器:"
pssh -i -h $iptxt_path  $update_code_path/cp_z.sh -u | tee $update_code_path/pssh-host
echo ''
echo 成功更新代码数:`cat $update_code_path/pssh-host | grep SUCCESS | wc -l`
last_t_continue "Is the revision right?"
pssh -i -h $iptxt_path $update_code_path/cp_z.sh -m > $md5_file_path/md5_on_price.md5
#cat $md5_file_path/md5_price.md5 | grep -v SUCCESS | grep -v ^$
echo 通过MD5游戏目录验证数:`cat $md5_file_path/md5_on_price.md5 | grep $FZ_MD5 | wc -l`
echo 未成功IP: `cat $md5_file_path/md5_on_price.md5 |awk 'BEGIN{a = "'$FZ_MD5'";} {if(NR %3 == 1) {b=$0;} if(NR%3 == 0) {if(a != $1){print  b;}}}'`

}
#_____________________________________________________________
#特殊服务器更新:应用宝 官网 封测
function t_y
{
echo -e "\033[31m 测试服更新: \033[0m"
json_config_name
echo $config_json > $release_path/config_json_t
server_path=$servert_path
server_go_path=$servert_go_path
server
gojson_config_name
echo $config_gojson > $release_path/config_gojson_t
server_go
iptxt_path=$hosts_path/t.txt
account_ip=$ts_ip
y_u
}

function w_y
{
echo -e "\033[31m 官网包更新: \033[0m"
server_path=$serverg_path
server
iptxt_path=$hosts_path/guanw.txt
account_ip=$ws_ip
y_u
}

function f_y
{
echo -e "\033[31m 封测服务器更新: \033[0m"

json_config_name
echo $config_json > $release_path/config_json_t
server_path=$serverf_path
server_go_path=$serverf_go_path
server
gojson_config_name
echo $config_gojson > $release_path/config_gojson_t
server_go
iptxt_path=$hosts_path/fengc.txt
account_ip=$fs_ip
y_u
}
#-------------------------------------------------------------

#_____________________________________________________________
function ios_y
{
echo -e "\033[31m IOS服务器更新: \033[0m"
server_path=$serveri_path    #替换server配置文件变量为IOS版本配置文件变量
server
iptxt_path=$hosts_path/ios_web.txt
account_ip=$ios_ip
y_u
}

function iosjb_y
{
echo -e "\033[31m IOS越狱服务器更新: \033[0m"
server_path=$serverjb_path
server
iptxt_path=$hosts_path/ios_jb.txt
account_ip=$iosjb_ip
y_u
}

function iosapp_y
{
echo -e "\033[31m IOS提审服务器更新: \033[0m"
server_path=$serveria_path
server
iptxt_path=$hosts_path/ios_app.txt
account_ip=$ios_app
y_u
}


#_____________________________________________________________
function apk_gc_y
{
echo -e "\033[31m 安卓公测服务器更新: \033[0m"
server_path=$servera_gc_path
server
iptxt_path=$hosts_path/apk_gcweb.txt
ipcodetxt_path=$hosts_path/apkcode_gc.txt
account_ip=$apk_gc_ip
y_u
}
function apk_tx_y
{
echo -e "\033[31m 安卓应用宝服务器更新: \033[0m"
server_path=$servera_tx_path
server
iptxt_path=$hosts_path/apk_txweb.txt
ipcodetxt_path=$hosts_path/apkcode_tx.txt
account_ip=$apk_tx_ip
y_u
}

#code代码服文件变量替换
#______________________________________________________________

#______________________________________________________________

#function on-line
#{
#pssh -i -h $hosts_path/ios.txt $update_code_path/cp.sh -u
#pssh -i -h $hosts_path/apk.txt $update_code_path/cp.sh -u
#}


#版本备份
function cgrb
{
echo -n "$date_file备份专服目录:"
ls -l $gameblank_path/$date_file | awk '{print $9}'
echo -n 请输入备份专服目录:
read Hb_dir
echo -e "\033[31m $Hb_dir: \033[0m"
echo ""
echo -n "$date_file/$Hb_dir专服版本目录:"
ls -l $gameblank_path/$date_file/$Hb_dir | awk '{print $9}'
echo ""
echo -n 输入版本库的更新版本号:
read Hb_t
echo -e "\033[31m $Hb_t: \033[0m"
last_t_continue "检查版本号是否正确:"
		if [ ! -d $gameblank_path/$date_file/$Hb_dir/$Hb_t ];then
                mkdir -p $gameblank_path/$date_file/$Hb_dir/$Hb_t
		else
		rm -rf $gameblank_path/$date_file/$Hb_dir/$Hb_t/*
                fi
cd $game_path
cp -r ./* $gameblank_path/$date_file/$Hb_dir/$Hb_t/
find ./ -type f -print0 | xargs -0 md5sum > $md5_file_path/mygame.md5

}

#版本回档
function rd_game
{
#echo -e "\033[31m 回档之前请先备份当前game目录: \033[0m"
#cgrb
#last_t_continue "是否进行回档?"
ls -l $gameblank_path | awk '{print $9}'
echo -n 输入回档日期:
read day
ls -l $gameblank_path/$day | awk '{print $9}'
echo -n 输入回档专服版本目录:
read vn_dir
ls -l $gameblank_path/$day/$vn_dir | awk '{print $9}'
echo -n 输入回档专服版本号:
read vn
last_t_continue "回档版本号为:$day/$vn_dir/$vn :"
cd $game_path
rm -rf $game_path/*
cp -r $gameblank_path/$day/$vn_dir/$vn/* $game_path/
}
 
function game_md5_price
{
echo -n rsync_game目录:$rsync_game_path 的MD值:' '  
cd $game_rsync_path
find ./ -type f -print0 | xargs -0 md5sum > $rsync_md5_path/game_md5_price.md5
cat $rsync_md5_path/game_md5_price.md5 | sort > $rsync_md5_path/md5_price.md5
#echo `md5sum $rsync_md5_path/md5_price.md5` | awk '{print $1}'
#echo -e "\033[31m `md5sum $rsync_md5_path/md5_price.md5 | awk '{print $1}'` \033[0m"
echo `md5sum $rsync_md5_path/md5_price.md5 | awk '{print $1}'`

}



function Nb
{
echo -n 输入要更新的版本号:
read Nb
last_t_continue "Is the revision right?"
rm -rf $release_path/$date_file/$Nb
echo $release_path/$date_file/$Nb
mkdir -p $release_path/$date_file/$Nb
echo 对测试服生成并更新版本:$Nb
release
}


function last_t_continue()
{
    echo -n "$@(y/n [n]):"
    read rett

    if [ $rett != "y" ]
    then
        echo "stop release"
        exit -1
    fi
}

function usage
{

        cat << EOU
        OPTIONS:
    	-h  	print this help
    	-u  	update game
    	-c  	create game release blank
	-t  	server_t_conf and rsync 
	-w  	server_guanwang_conf and rsync
    	-f  	server_fengce_conf and rsync
	-s	server_ios_conf and rsync
	-a	server_iosapp and rsync
	-j	server_iosjb_conf and rsync
	-k	server_apk_conf and rsync
	-g	server_apk_gc_conf and rsync
	-x	server_apk_tx_conf and rsync
	-r 	servergame archellovedrdes回档
	-p 	redis-cache同步
	-o	on-line
EOU
        exit
}


function update
{
echo ""
echo -n 输入版本库的更新版本号:
read Hb_t
echo $Hb_t
last_t_continue "检查版本号是否正确:"
cd $release_path/$date_file
tar -xf $Hb_t.tar
release_update
server_t
}


#远程PHP脚本操作区
#________________________________________________________________________________________________

#代码缓冲redis更新-远程执行php脚本
function php_code_run
{
pssh -i -h $ipcodetxt_path  $update_code_path/cp_z.sh -p > $md5_file_path/php-redis-run.txt
php_redis_code_sum=`cat $ipcodetxt_path |wc -l`
php_redis_cache_success_sum=`cat $md5_file_path/php-redis-run.txt | grep SUCCESS | wc -l`
php_redis_cache_ok_sum=`cat $md5_file_path/php-redis-run.txt | grep ok | wc -l`

}

function php_redis_tx
{
echo -n 应用宝代码redis-cache更新:
apk_tx_y
php_code_run
echo -e "\033[31m 应用宝代码redis-cache共操作:$php_redis_code_sum  完成操作:$php_redis_cache_success_sum  操作成功:$php_redis_cache_ok_sum \033[0m"
echo 未成功IP: `cat $md5_file_path/php-redis-run.txt |awk 'BEGIN{a = "'ok'";} {if(NR %3 == 1) {b=$0;} if(NR%3 == 0) {if(a != $2){print  b;}}}'`
}


function php_redis_gc
{
echo -n APK混服代码redis-cache更新:
echo T服代码redis-cache共操作:$php_redis_cache_ok_sum  完成操作:$php_redis_cache_success_sum  操作成功:$php_redis_cache_ok_sum 
}


function php_script
{
echo -n "请根据选项:
t):应用宝php_redis
g):混服php_redis
o):IOS服php_redis
输入操作:"
read php_ot
	last_t_continue "That all right?"
	case "$php_ot" in
	t) echo 应用宝php_redis; php_redis_tx ;;	
	g) echo 混服php_redis; php_redis_gc ;;
	o) echo IOS服php_redis; php_redis_ios ;;
	esac
}

#echo GAME目录:$game_path
pash_list
json_conf_rsync
#date_file
#md5_file_path
while getopts "huctwfsjkgxrmopa" opt; do
        case $opt in
        h) usage;;
        u) update;;
        c) cgrb;;
	t) t_y;;
	w) w_y;;
        f) f_y;;
	s) ios_y;;
	a) iosapp_y;;
	j) iosjb_y;;
	k) apk_y;;
	g) apk_gc_y;;
	x) apk_tx_y;;
	r) rd_game;;
	o) on-line;;
	m) game_md5_price;;
	p) php_script;;
	*) usage;;
        esac
done	
	
	
	
	
	
